﻿using FluentData;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeixinImageDownLoadServer
{
    /// <summary>
    /// 工作单元，统一事务控制
    /// </summary>
    public class UnitOfWork : IDisposable
    {
        private IDbContext context;
        private bool iscommit;
        //private bool istarn = true;
        public IDbContext Context
        {
            get { return context; }
        }

        public UnitOfWork()
        {
            context = new DbContext().ConnectionString(ConfigurationSettings.AppSettings["ConnectionString"], new MySqlProvider());
            context.UseTransaction(true);
        }

        public UnitOfWork(bool isTran)
        {
            context = new DbContext().ConnectionString(ConfigurationSettings.AppSettings["ConnectionString"], new MySqlProvider());
            if (isTran)
                context.UseTransaction(true);

        }

        public void Rollback()
        {
            context.Rollback();
        }

        public void Commit()
        {
            iscommit = true;
            context.Commit();
        }

        public void Dispose()
        {
            if (context != null)
            {
                if (!iscommit)
                {
                    context.Rollback();
                }
                context.Dispose();
            }

        }
    }
}
